package com.example.leaderapplication.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leaderapplication.Bean.FindShiftsListResultBean;
import com.example.leaderapplication.R;

import java.util.List;

public class BanCiListAdapter extends RecyclerView.Adapter<BanCiListAdapter.ViewHolder> {

    private List<FindShiftsListResultBean.DataBean.DataListBean> data;
    private Context context;
    private DeleteListener deleteListener;
    private ChangeListener changeListener;
    private ShipMessageListener shipMessageListener;

    public BanCiListAdapter(List<FindShiftsListResultBean.DataBean.DataListBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public BanCiListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.adapter_banci_show, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BanCiListAdapter.ViewHolder holder, int position) {

        holder.unitsNumber.setText(data.get(position).getShiftsName() + "");
        holder.unitsState.setText(data.get(position).getFlagStr() + "");
        holder.unitsName.setText(data.get(position).getBusinessOrgShortName() + "");
        holder.actionTime.setText(data.get(position).getShiftsDate() + "");
        holder.lineName.setText(data.get(position).getRouteName() + "");
        holder.shiftNumber.setText(data.get(position).getShiftsNo() + "");
        holder.shipName.setText(data.get(position).getShipName() + "");
        holder.peopleNumber.setText(data.get(position).getShipCapacity() + "");
        holder.haveNumber.setText(data.get(position).getShiftsCapacity() + "");
        holder.actionName.setText(data.get(position).getUpdatedUser() + "");
        holder.peopleTime.setText(data.get(position).getUpdatedDate() + "");

        holder.hasMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shipMessageListener.shipMessage(position);
            }
        });

        holder.adatperChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeListener.itemChange(position);
            }
        });

        holder.adatperDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteListener.itemDelete(position);
            }
        });
    }

    public interface DeleteListener{
        void itemDelete(int position);
    }

    public void setDeleteListener(DeleteListener delListener){
        deleteListener = delListener;
    }

    public interface ChangeListener{
        void itemChange(int position);
    }

    public void setChangeListener(ChangeListener chaListener){
        changeListener = chaListener;
    }

    public interface ShipMessageListener{
        void shipMessage(int postion);
    }

    public void setShipMessageListener(ShipMessageListener messageListener){
        shipMessageListener = messageListener;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView unitsNumber;
        private final TextView unitsState;
        private final TextView unitsName;
        private final TextView actionTime;
        private final TextView lineName;
        private final TextView shiftNumber;
        private final TextView shipName;
        private final TextView peopleNumber;
        private final TextView haveNumber;
        private final TextView actionName;
        private final TextView peopleTime;
        private final TextView hasMessage;
        private final Button adatperChange;
        private final Button adatperDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            unitsNumber = itemView.findViewById(R.id.tv_adapter_units_number);
            unitsState = itemView.findViewById(R.id.tv_adapter_units_state);
            unitsName = itemView.findViewById(R.id.tv_adapter_units);
            actionTime = itemView.findViewById(R.id.tv_adapter_action_time);
            lineName = itemView.findViewById(R.id.tv_adapter_action_line_name);
            shiftNumber = itemView.findViewById(R.id.tv_adapter_action_shift_number);
            shipName = itemView.findViewById(R.id.tv_adapter_action_ship_name);
            peopleNumber = itemView.findViewById(R.id.tv_adapter_have_people_number);
            haveNumber = itemView.findViewById(R.id.tv_adapter_have_number);
            actionName = itemView.findViewById(R.id.tv_adapter_action_name);
            peopleTime = itemView.findViewById(R.id.tv_adapter_action_people_time);
            hasMessage = itemView.findViewById(R.id.tv_adapter_message);
            adatperChange = itemView.findViewById(R.id.bt_adapter_change);
            adatperDelete = itemView.findViewById(R.id.bt_adapter_delete);
        }
    }
}
